package com.ktjy.goods.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.core.page.TableDataInfo;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.goods.domain.Goods;
import com.ktjy.goods.service.IGoodsService;
import com.ktjy.goods.util.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 规格属性Controller
 *
 * @author ruoyi
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/goods/attr")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService attrService;

/**
 * 查询规格属性列表
 */
    @PreAuthorize("@ss.hasPermi('goods:attr:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        System.out.println("goodsList()--------------------");
        startPage();
        List<Goods> list = attrService.selectAttrList(goods);
        return getDataTable(list);
    }

    private static final String ROOT_PATH=System.getProperty("user.dir")+ File.separator+"files";

    @PostMapping("/upload")
    public ResultUtil<String> upload(MultipartFile file) {
        System.out.println("文件上传.............file="+file);
        System.out.println("文件上传路径.............ROOT_PATH="+ROOT_PATH);

        String originalFilename = file.getOriginalFilename();   //获取文件的原始名称
        System.out.println("originalFilename="+originalFilename);


        try {
            //构建路径：E:\idea_workSpace\360\vuespringboot\files\aaa.png
            File saveFile = new File(ROOT_PATH+File.separator+originalFilename);
            file.transferTo(saveFile);     //存储文件到本地地磁盘
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error("文件上传失败~");
        }

        return ResultUtil.success("文件上传成功~");
    }


    /**
     * 导出规格属性列表
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:export')")
    @Log(title = "规格属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = attrService.list();
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "规格属性数据");
    }

    /**
     * 获取规格属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attrService.getById(id));
    }

    /**
     * 新增规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:add')")
    @Log(title = "规格属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        return toAjax(attrService.save(goods));
    }

    /**
     * 修改规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:edit')")
    @Log(title = "规格属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(attrService.updateById(goods));
    }

    /**
     * 删除规格属性
     */
    @PreAuthorize("@ss.hasPermi('goods:attr:remove')")
    @Log(title = "规格属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids)
    {
        return toAjax(attrService.removeByIds(ids));
    }


    @Log(title = "规格管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('goods:attr:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        List<Goods> goodsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = attrService.importUser(goodsList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.importTemplateExcel(response, "规格数据");
    }
}